package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.dto.ArticleDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.IApArticleContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP已发布文章内容表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-29
 */
@Service
public class ApArticleContentServiceImpl extends ServiceImpl<ApArticleContentMapper, ApArticleContent> implements IApArticleContentService {


    @Autowired
    private IApArticleContentService apArticleContentService;

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        //如果存在,修改
        if(dto.getId()!=null){
//            修改的操作
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
//            apArticle中的id置成null
            LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper();
            updateWrapper.eq(ApArticleContent::getArticleId,apArticle.getId());
            updateWrapper.set(ApArticleContent::getContent,dto.getContent());
            apArticleContentService.update(updateWrapper);
            return ResponseResult.okResult(apArticle.getId());
        }else{
//            新增的操作
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
            apArticle.setId(null);
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setId(null);
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);
            return ResponseResult.okResult(apArticle.getId());
        }
    }
    }

